var entityCollection = require('../../../Base/server/controllers/EntityCollection'),
    itemMasterController = require('./ItemMaster.server.controller');

//根据根据工艺路线查找全部工序
exports.findByItemMaster = function(itemMasterId, completeFun){
    var entity = entityCollection.getEntity("Routing");
	entity.findOne({ItemMaster: itemMasterId}, {path: 'ItemMaster'}, function (err, record) {
        if (err) {
            var newErr = new Error();
            newErr.leval = 9;
            newErr.title = '查询工艺路线错误';
            newErr.message = '查询失败，请检查查询参数！';
            completeFun(newErr);
            return;
        }
        if (!record) {
            itemMasterController.findById(itemMasterId, { ItemCategory: 1 }).then(function (result) {
                entity.findOne({ItemCategory: result.ItemCategory}, {path: 'ItemMaster'}, function (err, record) {
                    if (err) {
                        var newErr = new Error();
                        newErr.leval = 9;
                        newErr.title = '查询工艺路线错误';
                        newErr.message = '查询失败，请检查查询参数！';
                        completeFun(newErr);
                        return;
                    }
                    completeFun(null, record);
                });
            }, function (err) {
                completeFun(err);
            });
            return;
        }
        completeFun(null, record);
    });
};
